package com.dunke.web.handler;

import com.dunke.core.common.Result;
import com.dunke.core.enums.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class InterfaceExceptionHandler {

    /**
     * 拦截所有运行时的全局异常   
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result runtimeException(RuntimeException e) {
//        log.error(e.getMessage(), e);
        // 返回 JOSN
        return Result.error(ReturnCode.UNUSUAL);
    }

    /**
     * 系统异常捕获处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exception(Exception e) {
//        log.error(e.getMessage(), e);
        // 登录异常校验
        if (e.getMessage().equals(ReturnCode.USER_TOKEN_UNUSUAL.getMessage())) {
            return Result.error(ReturnCode.USER_TOKEN_UNUSUAL);
        }
        return Result.error(ReturnCode.UNUSUAL);

    }

}
